package com.product.system.controller;


import com.product.system.domain.response.ArgpHoliday;
import com.product.system.domain.response.ArgpHolidayLog;
import com.product.system.service.ArgpHolidayService;
import com.product.system.utils.Result;
import org.activiti.engine.*;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.apache.catalina.security.SecurityUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("holiday")
public class ArgpHolidayActivitiController {



    @Autowired
    RepositoryService repositoryService;

    @Autowired
    TaskService taskService;

    @Autowired
    HistoryService historyService;

    @Autowired
    RuntimeService runtimeService;

    @Autowired
    ArgpHolidayService argpHolidayService;


    @PostConstruct
    public void testDeployment(){
        // 3.使用service进行流程部署 ，定义流程名，把 bpmn 和 png 部署到数据库
        ProcessDefinition argpapplyholiday = repositoryService.createProcessDefinitionQuery().processDefinitionKey("argpapplyholiday").singleResult();
        if (argpapplyholiday == null) {
            repositoryService.createDeployment()
                    .name("请假申请流程")
                    .addClasspathResource("bpmn/argpapplyholiday.bpmn20.xml")
                    .deploy();
        } else {
            System.out.println("流程已部署");
        }

    }


    //开始提交审核
    @GetMapping("sendApply")
    public Result sendApply(Integer id) {

        ArgpHoliday argpHoliday = argpHolidayService.selectArgpHoliday(id);

        HashMap<String, Object> map = new HashMap<>();
        map.put("creatholidayform","admin");//创建申请
        map.put("managerapprove","ry");//经理审批
        map.put("personnelrapprove","admin2");//人事审批
        map.put("boosrapprove","boos");//老板审批holidaytime
        map.put("holidaytime",argpHoliday.getLeaveDuration());//天数

        Object leaveDuration = argpHoliday.getLeaveDuration();
        System.out.println("Leave duration value: " + leaveDuration);
        System.out.println("Leave duration type: " + leaveDuration.getClass().getName());

        ProcessInstance argpapplyholiday = runtimeService.startProcessInstanceByKey("argpapplyholiday", argpHoliday.getId() + "", map);

        Task task = taskService.createTaskQuery().taskAssignee("admin").processInstanceId(argpapplyholiday.getId()).singleResult();

        if (argpapplyholiday != null) {
           Boolean b =  argpHolidayService.updataStatusWait(argpHoliday.getId());
           taskService.complete(task.getId(),map);
            return b?Result.success():Result.fail();
        }

        return Result.fail();
    }

    //我的待办列表
    @GetMapping("dealtDataList")
    public Result dealtDataList() {

        List<Task> list = taskService.createTaskQuery().processDefinitionKey("argpapplyholiday").taskAssignee("ry").list();

        ArrayList<ArgpHoliday> argpHolidays = new ArrayList<>();
//        ArgpHoliday argpHoliday = new ArgpHoliday();
        for (Task task : list) {

            //获取业务key
            String businessKey = historyService.createHistoricProcessInstanceQuery()
                    .processInstanceId(task.getProcessInstanceId())
                    .singleResult()
                    .getBusinessKey();
            ArgpHoliday one = argpHolidayService.selectArgpHoliday(Integer.valueOf(businessKey));



            one.setInstanceId(task.getProcessInstanceId());
            one.setTaskId(task.getId());
            one.setTaskAssignee(task.getAssignee());
            one.setTaskName(task.getName());

            argpHolidays.add(one);

        }
        return Result.success(argpHolidays);
    }


    //添加请假申请
    @PostMapping("insertHoliday")
    public Result startActiviti(@RequestBody ArgpHoliday argpHoliday) {
        Boolean i = argpHolidayService.insertArgpHoliday(argpHoliday);
        return i?Result.success("添加成功"):Result.fail();
    }


    //申请列表查询
    @GetMapping("applyDatadList")
    public Result applyDatadList() {
        List<ArgpHoliday> argpHolidays = argpHolidayService.selectHolidayList();
        return Result.success(argpHolidays);
    }


    //查询任务列表
//    @GetMapping("selectHolidayList")
//    public Result selectHolidayList() {
//
//
//        List<Task> list = taskService.createTaskQuery().processDefinitionKey("argpapplyholiday").taskAssignee("admin").list();
//
//
//        ArrayList<ArgpHoliday> argpHolidays = new ArrayList<>();
//        ArgpHoliday argpHoliday = new ArgpHoliday();
//        for (Task task : list) {
//            //获取任务实例id
//            String processInstanceId = task.getProcessInstanceId();
//
//            //获取业务id
//            String businessKey = historyService.createHistoricProcessInstanceQuery()
//                    .processInstanceId(processInstanceId)
//                    .singleResult()
//                    .getBusinessKey();
//
//            ArgpHoliday argpHoliday1 = argpHolidayService.selectArgpHoliday(Integer.valueOf(businessKey));
//
//            argpHoliday.setId(argpHoliday1.getId());
//            argpHoliday.setEmployeeName(argpHoliday1.getEmployeeName());
//            argpHoliday.setEmployeeId(argpHoliday1.getEmployeeId());
//            argpHoliday.setPosition(argpHoliday1.getPosition());
//            argpHoliday.setLeaveTypeId(argpHoliday1.getLeaveTypeId());
//            argpHoliday.setStartTime(argpHoliday1.getStartTime());
//            argpHoliday.setEndTime(argpHoliday1.getEndTime());
//            argpHoliday.setLeaveDuration(argpHoliday1.getLeaveDuration());
//            argpHoliday.setLeaveReason(argpHoliday1.getLeaveReason());
//            argpHoliday.setHandoverTo(argpHoliday1.getHandoverTo());
//            argpHoliday.setHandoverDescription(argpHoliday1.getHandoverDescription());
//            argpHoliday.setDirectManagerApprovalTime(argpHoliday1.getDirectManagerApprovalTime());
//
//            argpHoliday.setTaskId(task.getId());
//            argpHoliday.setTaskName(task.getName());
//            argpHoliday.setTaskAssignee(task.getAssignee());
//            argpHoliday.setInstanceId(processInstanceId);
//
//            argpHolidays.add(argpHoliday);
//
//        }
//
//        return Result.success(argpHolidays);
//    }


    @PostMapping("sureDealt")
    public void complateHoliday(@RequestBody ArgpHolidayLog argpHolidayLog) {

        System.out.println("aaaaaaaaaaaaaaaaaaaaaaaaaaaa");


        taskService.complete(argpHolidayLog.getTaskId());

        argpHolidayLog.setStatus(4);
        argpHolidayLog.setDealtTime(new Date());
       Boolean a =  argpHolidayService.insertArgpHolidayLog(argpHolidayLog);

    }








}
